package com.huaxin.device.mapping;

import com.huaxin.device.dto.enums.ConnectionType;
import com.huaxin.device.dto.enums.EquipmentType;
import com.huaxin.device.dto.enums.PhaseCount;
import com.huaxin.device.dto.enums.VoltageLevel;
import com.huaxin.device.dto.request.EquipmentConnectionsCreateReqDTO;
import com.huaxin.device.dto.request.EquipmentConnectionsUpdateReqDTO;
import com.huaxin.device.models.EquipmentConnection;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;

@Mapper(componentModel = "spring")
public interface EquipmentConnectionsMapping {

    @Mapping(target = "fromEquipmentType", ignore = true)
    @Mapping(target = "toEquipmentType", ignore = true)
    @Mapping(target = "connectionType", ignore = true)
    @Mapping(target = "voltageLevel", ignore = true)
    @Mapping(target = "phaseCount", ignore = true)
    EquipmentConnection toEntity(EquipmentConnectionsCreateReqDTO in);

    @AfterMapping
    default void afterMapping(EquipmentConnectionsCreateReqDTO in, @MappingTarget EquipmentConnection entity) {
        entity.setFromEquipmentType(EquipmentType.of(in.getFromEquipmentType()));
        entity.setToEquipmentType(EquipmentType.of(in.getToEquipmentType()));
        if (in.getConnectionType() != null) {
            entity.setConnectionType(ConnectionType.of(in.getConnectionType()));
        }
        if (in.getVoltageLevel() != null) {
            entity.setVoltageLevel(VoltageLevel.of(in.getVoltageLevel()));
        }
        if (in.getPhaseCount() != null) {
            entity.setPhaseCount(PhaseCount.of(in.getPhaseCount()));
        }
    }

    @Mapping(target = "fromEquipmentType", ignore = true)
    @Mapping(target = "toEquipmentType", ignore = true)
    @Mapping(target = "connectionType", ignore = true)
    @Mapping(target = "voltageLevel", ignore = true)
    @Mapping(target = "phaseCount", ignore = true)
    void updateEntity(EquipmentConnectionsUpdateReqDTO in, @MappingTarget EquipmentConnection entity);

    @AfterMapping
    default void afterMapping(EquipmentConnectionsUpdateReqDTO in, @MappingTarget EquipmentConnection entity) {
        entity.setFromEquipmentType(EquipmentType.of(in.getFromEquipmentType()));
        entity.setToEquipmentType(EquipmentType.of(in.getToEquipmentType()));
        if (in.getConnectionType() != null) {
            entity.setConnectionType(ConnectionType.of(in.getConnectionType()));
        }
        if (in.getVoltageLevel() != null) {
            entity.setVoltageLevel(VoltageLevel.of(in.getVoltageLevel()));
        }
        if (in.getPhaseCount() != null) {
            entity.setPhaseCount(PhaseCount.of(in.getPhaseCount()));
        }
    }
}